﻿using Breeze.ContextProvider.EF6;
using RecruitmentPage.Models;
using RecruitmentPage.Models.RPModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace RecruitmentPage.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult JobDetail()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }

        public void SetIdCookie(string accoutid , string personalid)
        {
            HttpCookie cookie1 = new HttpCookie("_accid",accoutid );
            Response.Cookies.Add(cookie1);

            HttpCookie cookie2 = new HttpCookie("_perid", personalid);
            Response.Cookies.Add(cookie2);
        }

        public void SetRoleCookie(RPContext db, Guid param)
        {
            var roles = (from a in db.Role where a.UniqueId == param  select a).FirstOrDefault();
            HttpCookie cookie3 = new HttpCookie("_role", roles.Name.ToString());
            Response.Cookies.Add(cookie3);

            var menus = from m in db.Menu
                        join rm in db.RoleMenu on m.UniqueId equals rm.MenuId
                        where rm.RoleId == roles.UniqueId
                        select new
                        {
                            name = m.Method
                        };
            string method = "";
            foreach (var pair in menus)
            {
                if (method == "") method += pair.name;
                else method += "," + pair.name;
            }
            HttpCookie cookie4 = new HttpCookie("_method", method);
            Response.Cookies.Add(cookie4);
        }

        [HttpPost]
        public ActionResult Login(Account Account)
        {
            RPContext db = new RPContext();

            var accounts = (from a in db.Account where a.Email == Account.Email && a.Password == Account.Password select a).FirstOrDefault();
            if (accounts != null)
            {
                if (accounts.Status == 0)
                {
                    ModelState.AddModelError("", "This account is not active!");
                    return View(Account);
                }
                if (accounts.Status == 2)
                {
                    ModelState.AddModelError("", "This account is disabled!");
                    return View(Account);
                }
                FormsAuthentication.SetAuthCookie(accounts.Email, true);

                SetIdCookie(accounts.UniqueId.ToString(), accounts.PersonalId.ToString());              
                SetRoleCookie(db, accounts.RoleId);                

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Login failed!");
                
                return View(Account);
            }
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }


        public ActionResult Register()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        [HttpPost]
        public int checkExist(string email)
        {
            RPContext db = new RPContext();

            var accounts = (from a in db.Account where a.Email == email select a).FirstOrDefault();
            if (accounts != null)
            {
                return 0;
            }
            else
            {
                return 1;
            }
        }
    }
}